﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Transactions;
using ThanNhanTao.Server.Entities;
using System.Data;
using System.Data.SqlClient;
using System.Data.Objects.SqlClient;

namespace ThanNhanTao.Server.BLL
{
    public partial class HoSoNhanSuBLL
    {
        bvdn_thannhantaoEntities entities;
        public HoSoNhanSuBLL()
        {
            entities = ThanNhanTaoGlobalServer.bvdn_thannhantaoEntities;
        }

        public List<HoSoNhanSuEntities> RetrieveAllHoSoNhanSu()
        {
            List<HoSoNhanSuEntities> list = new List<HoSoNhanSuEntities>();
            list = (from a in entities.HoSoNhanSus
                    orderby a.Ten ascending
                    select new HoSoNhanSuEntities
                    {
                        IdHoSoNhanSu = a.IdHoSoNhanSu,
                        Ho = a.Ho,
                        Ten = a.Ten,
                        GioiTinh = a.GioiTinh,
                        NgaySinh = a.NgaySinh,
                        NamSinh = a.NamSinh,
                        Tuoi = a.Tuoi,
                        MaDanToc = a.Dm_DanToc != null ? a.Dm_DanToc.MaDanToc : "",
                        TenDanToc = a.Dm_DanToc != null ? a.Dm_DanToc.TenDanToc : "",
                        TinhTrangHonNhan = a.TinhTrangHonNhan,
                        DiaChiThuongTru = a.DiaChiThuongTru,
                        NguyenQuan = a.NguyenQuan,
                        SoHieuCongChuc = a.SoHieuCongChuc,
                        DienThoaiDiDong = a.DienThoaiDiDong,
                        DiaChiEmail = a.DiaChiEmail,
                        LaDangVien = a.LaDangVien,
                        NgayKetNapDang = a.NgayKetNapDang,
                        NgayChinhThucVaoDang = a.NgayChinhThucVaoDang,
                        ChucVuDang = a.ChucVuDang
                    }).ToList(); ;
            return list;
        }

        public HoSoNhanSuEntities InPhieuThongTinHoSoCongChucVienChuc(int idHoSoNhanSu)
        {
            return (from a in entities.HoSoNhanSus
                    where a.IdHoSoNhanSu == idHoSoNhanSu
                    orderby a.Ten ascending
                    select new HoSoNhanSuEntities
                    {
                        IdHoSoNhanSu = a.IdHoSoNhanSu,
                        Ho = a.Ho,
                        Ten = a.Ten,
                        GioiTinh = a.GioiTinh,
                        NgaySinh = a.NgaySinh,
                        NamSinh = a.NamSinh,
                        Tuoi = a.Tuoi,
                        MaDanToc = a.Dm_DanToc != null ? a.Dm_DanToc.MaDanToc : "",
                        TenDanToc = a.Dm_DanToc != null ? a.Dm_DanToc.TenDanToc : "",
                        TinhTrangHonNhan = a.TinhTrangHonNhan,
                        DiaChiThuongTru = a.DiaChiThuongTru,
                        NguyenQuan = a.NguyenQuan,
                        SoHieuCongChuc = a.SoHieuCongChuc,
                        DienThoaiDiDong = a.DienThoaiDiDong,
                        DiaChiEmail = a.DiaChiEmail,
                        LaDangVien = a.LaDangVien,
                        NgayKetNapDang = a.NgayKetNapDang,
                        NgayChinhThucVaoDang = a.NgayChinhThucVaoDang,
                        ChucVuDang = a.ChucVuDang
                    }).FirstOrDefault();
        }

        public HoSoNhanSuEntities RetrieveHoSoNhanSu(int idHoSoNhanSu)
        {
            return (from a in entities.HoSoNhanSus
                    where a.IdHoSoNhanSu == idHoSoNhanSu
                    orderby a.Ten ascending
                    select new HoSoNhanSuEntities
                    {
                        Ho = a.Ho,
                        Ten = a.Ten,
                        GioiTinh = a.GioiTinh,
                        NgaySinh = a.NgaySinh,
                        NamSinh = a.NamSinh,
                        Tuoi = a.Tuoi,
                        MaDanToc = a.Dm_DanToc != null ? a.Dm_DanToc.MaDanToc : "",
                        TenDanToc = a.Dm_DanToc != null ? a.Dm_DanToc.TenDanToc : "",
                        TinhTrangHonNhan = a.TinhTrangHonNhan,
                        DiaChiThuongTru = a.DiaChiThuongTru,
                        NguyenQuan = a.NguyenQuan,
                        SoHieuCongChuc = a.SoHieuCongChuc,
                        DienThoaiDiDong = a.DienThoaiDiDong,
                        DiaChiEmail = a.DiaChiEmail,
                        LaDangVien = a.LaDangVien,
                        NgayKetNapDang = a.NgayKetNapDang,
                        NgayChinhThucVaoDang = a.NgayChinhThucVaoDang
                    }).FirstOrDefault();
        }

        public void AddHoSoNhanSu(ref HoSoNhanSuEntities hoSo)
        {
            HoSoNhanSu nhanSu = new HoSoNhanSu();
            nhanSu.Ho = hoSo.Ho;
            nhanSu.Ten = hoSo.Ten;
            nhanSu.GioiTinh = hoSo.GioiTinh;
            if (hoSo.NgaySinh != null)
                nhanSu.NgaySinh = hoSo.NgaySinh;
            nhanSu.NamSinh = hoSo.NamSinh;
            nhanSu.Tuoi = hoSo.Tuoi;
            nhanSu.MaDanToc = hoSo.MaDanToc;
            nhanSu.TinhTrangHonNhan = hoSo.TinhTrangHonNhan;
            nhanSu.DiaChiThuongTru = hoSo.DiaChiThuongTru;
            nhanSu.NguyenQuan = hoSo.NguyenQuan;
            nhanSu.SoHieuCongChuc = hoSo.SoHieuCongChuc;
            nhanSu.DienThoaiDiDong = hoSo.DienThoaiDiDong;
            nhanSu.DiaChiEmail = hoSo.DiaChiEmail;
            nhanSu.LaDangVien = hoSo.LaDangVien;
            if (hoSo.NgayKetNapDang != null)
                nhanSu.NgayKetNapDang = hoSo.NgayKetNapDang;
            if (hoSo.NgayChinhThucVaoDang != null)
                nhanSu.NgayChinhThucVaoDang = hoSo.NgayChinhThucVaoDang;
            nhanSu.ChucVuDang = hoSo.ChucVuDang;
            entities.AddToHoSoNhanSus(nhanSu);
            if(entities.SaveChanges() > 0)
                hoSo.IdHoSoNhanSu = nhanSu.IdHoSoNhanSu;
        }

        public bool UpdateHoSoNhanSu(HoSoNhanSuEntities hoSo)
        {
            HoSoNhanSu nhanSu = entities.HoSoNhanSus.FirstOrDefault(u => u.IdHoSoNhanSu == hoSo.IdHoSoNhanSu);
            if (nhanSu != null)
            {
                nhanSu.IdHoSoNhanSu = hoSo.IdHoSoNhanSu;
                nhanSu.Ho = hoSo.Ho;
                nhanSu.Ten = hoSo.Ten;
                nhanSu.GioiTinh = hoSo.GioiTinh;
                nhanSu.NgaySinh = hoSo.NgaySinh;
                nhanSu.NamSinh = hoSo.NamSinh;
                nhanSu.Tuoi = hoSo.Tuoi;
                nhanSu.MaDanToc = hoSo.MaDanToc;
                nhanSu.TinhTrangHonNhan = hoSo.TinhTrangHonNhan;
                nhanSu.DiaChiThuongTru = hoSo.DiaChiThuongTru;
                nhanSu.NguyenQuan = hoSo.NguyenQuan;
                nhanSu.SoHieuCongChuc = hoSo.SoHieuCongChuc;
                nhanSu.DienThoaiDiDong = hoSo.DienThoaiDiDong;
                nhanSu.DiaChiEmail = hoSo.DiaChiEmail;
                nhanSu.LaDangVien = hoSo.LaDangVien;
                nhanSu.NgayKetNapDang = hoSo.NgayKetNapDang;
                nhanSu.NgayChinhThucVaoDang = hoSo.NgayChinhThucVaoDang;
                nhanSu.ChucVuDang = hoSo.ChucVuDang;
                return (entities.SaveChanges() > 0);
            }
            return false;
        }

        public void DeleteHoSoNhanSu(int idHoSoNhanSu)
        {
            HoSoNhanSu nhanSu = entities.HoSoNhanSus.FirstOrDefault(u => u.IdHoSoNhanSu == idHoSoNhanSu);
            entities.DeleteObject(nhanSu);
            entities.SaveChanges();
        }
    }
}
